﻿/*----------------------------------------------------------------
//Copyright (C) 2012 赵国伟
//文件功能描述：商品控件选择脚本
//创建人：赵国伟
//生成时间：2012-04-08 15:52:29
//----------------------------------------------------------------*/


/*
relateObject:绑定的目标对象ID, 如文本框，按钮等
selectMode: 选择模式：分为单选模式和多选模式 Single为单选模式 Muti为多选模式。
outSpan://选中值的输出标签ID;
defaultData; //默认值。
*/
var SelectGoods = function (relateObject, selectMode, outSpan, defaultData, callBack) {
    this.ServerUrl = "/Manage/AS/GoodsCommon.aspx?action=QueryGoodsAsPage"; //获取会员数据的Ajax地址；
    this.ObjName = "selectOf" + relateObject;
    this.InputControl = document.getElementById(relateObject);
    this.InputControlName = relateObject;
    this.InputControl.readOnly = true; //设置表单只可只读；
    this.PopDivName = "divOf" + relateObject; //浮层对象名称；
    this.KeywordsInputName = "txtSearchOf" + relateObject; //浮层中的关键字搜索表单；
    this.SearchResultSpanName = "spanResultOf" + relateObject; //浮层中显示查询结果的Span标签对象；
    this.SelectMode = selectMode; //选择的模式：Single=单选模式；Muti=多选模式。
    this.CallBack = callBack;
    this.CurPage = 1; //当前页面；
    this.PageSize = 20; //每页记录数。
    this.TotalCount = 0; //记录总数；
    this.OutSpan = "";
    if (outSpan != undefined) {
        this.OutSpan = outSpan;
    }
    this.Data = new HashTable();
    this.SelectNames = new Array();
    this.SelectIds = new Array();
    this.SetDefaultValue(defaultData);

    var thisObj = this;
    this.InputControl.onclick = function () {   //为选择框绑定onclick事件
        thisObj.CreateSelectDiv(); //创建选择层；
    };
    this.InputControl.onmouseout = function () {
        thisObj.CloseSelectDiv();
    }
    SelectGoods.instances[this.ObjName] = this;
    SelectGoods.CurrentInstance = this; //记录下当前实例；
}

//存储实例对象
SelectGoods.instances = {};
SelectGoods.CurrentInstance = null; //存储当前实例；

SelectGoods.GetInstance = function (objName) {
    return SelectGoods.instances[objName];
}

///设置默认值；
SelectGoods.prototype.SetDefaultValue = function (defaultData) {
    if (defaultData != null && defaultData != undefined) {
        for (var i = 0; i < defaultData.length; ++i) {
            this.SelectNames.push(defaultData[i]._name);
            this.SelectIds.push(defaultData[i]._id);
        }
        this.OutToSpan();
    }
}

///获得控件的选中值；
SelectGoods.prototype.SelectValue = function () {
    return this.SelectIds.join(",");
}

SelectGoods.prototype.SelectValueEx = function () {
    var ds = new Array();
    var idname  = { "_id": 0, "_name": null };
    for (var i = 0; i < this.SelectIds.length; ++i) {
        var d = clone(idname);
        d._id = this.SelectIds[i];
        d._name = this.SelectNames[i];
        ds.push(d);
    }
    return ds;
}

//删除指定的选项。
SelectGoods.prototype.Remove = function (id, objName) {
    var thisControl = SelectGoods.GetInstance(objName);
    thisControl.SelectNames.remove(id);
    thisControl.SelectIds.remove(id);
    thisControl.OutToSpan(); //输出到指定的SPAN标签中。
}


//将选中的值输出到Span标签中。
SelectGoods.prototype.OutToSpan = function () {
    if (this.OutSpan == "")
        return;
    var result = "";
    for (var i = 0; i < this.SelectIds.length; ++i) {
        result += this.SelectNames[i] + "<a href=\"javascript:SelectGoods.CurrentInstance.Remove(" + this.SelectIds[i] + ",'" + this.ObjName + "');\" title=\"点击删除本条？\"><img src=\"/Manage/images/cha.jpg\" ></a> &nbsp;";
    }
    oo(this.OutSpan).innerHTML = result;
}


//设置选中状态；
SelectGoods.prototype.SelectItem = function (name, id) {
    var spanName = "sp" + this.ObjName + "Of" + id;
    if (this.SelectMode == "Single") {
        this.SelectNames = new Array();
        this.SelectIds = new Array();
    }
    if (oo(spanName).innerHTML == "") {
        this.SelectNames.push(name);
        this.SelectIds.push(id);
        oo(spanName).innerHTML = "√";
    }
    else {
        this.SelectNames.remove(id);
        this.SelectIds.remove(id);
        oo(spanName).innerHTML = "";
    }
    this.OutToSpan(); //输出到指定的SPAN标签中。
    if (this.SelectMode == "Single") {
        if (this.CallBack != undefined && this.CallBack != null) {
            this.CallBack(this.Data.items(id).BaseInfo._goodsid, this.Data.items(id).BaseInfo._goodsname, this.Data.items(id).BaseInfo._saleprice, this.Data.items(id).PackUnit, this.Data.items(id).BaseInfo._packunitid);
        }
        this.CloseSelectDiv(true); //单选模式下直接关闭选择浮层；
    }
}


///创建签约公司选择下拉层；
SelectGoods.prototype.CreateSelectDiv = function () {
    if (oo(this.PopDivName) != null)
        return;
    //该输入框控件处创建层；
    var o = document.createElement('div');
    o.id = this.PopDivName;   //提示层对象名称；
    o.className = "SeniorSelect";  //提示层样式；
    o.style.width = "600px";
    document.body.appendChild(o);
    o.innerHTML = "";
    o.style.zIndex = "10999";
    var thisObj = this;
    o.onmouseout = function () {
        thisObj.CloseSelectDiv();
    };

    var html = new Array();

    html.push("<table width=\"100%\"><tr><td bgcolor=\"#D4ECF9\">");
    html.push("<span class=\"Title\">关键词：<input type=\"text\" id=\"" + this.KeywordsInputName + "\" autocomplete=\"off\" class=\"border2\" size=\"40\" onkeyup=\"SelectGoods.CurrentInstance.GenerateDivContent()\">&nbsp;&nbsp;</span>");
    html.push("</td></tr></table>");
    html.push("<span id=\"" + this.SearchResultSpanName + "\">");

    html.push("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
    html.push("<tr><td align=\"right\" valign=\"middle\" width=\"40%\" > ");
    html.push("<img src=\"/Manage/images/waiting.gif\" width=\"71\" height=\"83\" >");
    html.push("</td>");
    html.push("<td align=\"left\" valign=\"middle\" > 数据加载中...</td>");
    html.push("</tr></table>");

    html.push("</span>");
    o.innerHTML = html.join("");

    //设置该层的位置；
    var sWidth = document.body.clientWidth;
    if (sWidth < 1000)
        sWidth += 20;

    var sScrollLeft = getScrollLeft(this.PopDivName);
    var sLeft = getLeft(this.InputControl);   //左侧坐标；
    var sTop = getTop(this.InputControl);

    //获取当前浮动层的宽度；
    var curWidth = sLeft - sScrollLeft + o.offsetWidth;
    if (curWidth > sWidth) {
        //浮层的宽度超过屏幕宽度 出现滚动条的情况; 移动该浮层的坐标；
        o.style.left = (sLeft - (curWidth - sWidth)) - sScrollLeft + "px";
    }
    else {
        o.style.left = sLeft - sScrollLeft + "px";
    }
    o.style.top = sTop + this.InputControl.offsetHeight + "px";
    this.GenerateDivContent();
}


//生成下拉层的内容；
SelectGoods.prototype.GenerateDivContent = function () {
    var curControl = this; //记录当前控件指针。
    IsShowTaskTipDiv = false; //关闭浮层提示窗；
    $.ajax(
    {
        type: "Get",
        url: curControl.ServerUrl + "&keywords=" + escape($("#" + curControl.KeywordsInputName).val()) + "&curPage=" + curControl.CurPage + "&pageMaxRecord=" + curControl.PageSize + "&tttt=" + Math.random(),
        dataType: "json",
        success: function (result) {
            var html = new Array();
            html.push("<table border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"#cccccc\" class=\"table\" >");
            if (result != null && result.Data != null) {
                html.push("<tr >");
                curControl.TotalCount = result.TotalCount; //设置记录总数；
                for (var i = 0; i < result.Data.length; ++i) {
                    curControl.Data.set(result.Data[i].BaseInfo._goodsid, result.Data[i]);
                    html.push("<td  bgcolor=\"#FFFFFF\" width=\"50%\" onclick=\"SelectGoods.CurrentInstance.SelectItem('" + result.Data[i].BaseInfo._goodsname + "','" + result.Data[i].BaseInfo._goodsid + "');\" onMouseOver=\"this.style.backgroundColor='#E6F5FA';\" onMouseOut=\"this.style.backgroundColor='#FFFFFF'\"; style=\"cursor:hand;\">&nbsp;" + ((curControl.CurPage - 1) * curControl.PageSize + i + 1) + "." + result.Data[i].BaseInfo._goodsname);
                    html.push("<span id=\"sp" + curControl.ObjName + "Of" + result.Data[i].BaseInfo._goodsid + "\" style=\"color:red;font-weight:bold;\">");

                    if (curControl.SelectIds.Contains(result.Data[i].BaseInfo._goodsid)) {
                        html.push("√");
                    }
                    html.push("</span>");
                    html.push("</td>");
                    if ((i + 1) % 2 == 0) {
                        html.push("</tr>");
                        if ((i + 1) < result.Data.length)
                            html.push("<tr >");
                    }
                }
                if ((result.Data.length % 2) > 0) {
                    for (var i = 0; i < (result.Data.length % 2); ++i)
                        html.push("<td bgcolor=\"#FFFFFF\">&nbsp;</td>");
                }
                html.push("</tr>");
                html.push("<tr  bgcolor=\"#FFFFFF\"><td colspan=2 align=center><span class=\"page\">");
                html.push(curControl.GetPageLine(curControl.CurPage, 20, 5, result.TotalCount));
                html.push("</span></td></tr>");
            }
            else {
                html.push("<tr  bgcolor=\"#FFFFFF\"><td colspan=2 align=center class=\"red\">[ 未找到对应的数据！ ] </td></tr>");

            }
            html.push("</table>");
            var HtmlContent = html.join("");
            oo(curControl.SearchResultSpanName).innerHTML = HtmlContent;
        }
    });
    IsShowTaskTipDiv = true;
}


//销毁进行选择的下拉层；
SelectGoods.prototype.CloseSelectDiv = function (IsNowClose) {

    if (IsNowClose == undefined || IsNowClose != true)
        IsNowClose = false;
    var obj = oo(SelectGoods.CurrentInstance.PopDivName);
    if (obj == null) return;
    if (IsNowClose) {
        document.body.removeChild(obj);
    }
    else {

        if (obj.componentFromPoint(event.clientX, event.clientY) == "outside") {
            document.body.removeChild(obj);
        }
    }
}





///输出指定的分页信息；
SelectGoods.prototype.GetPageLine = function (curPage, pageMaxRecord, countPerPage, totalCount) {
    totalPage = parseInt(totalCount % pageMaxRecord > 0 ? totalCount / pageMaxRecord + 1 : totalCount / pageMaxRecord);
    //处理页标越界的情况；
    if (curPage <= 0) curPage = 1;
    if (curPage > totalPage) curPage = totalPage;
    var curLinkPage = parseInt((curPage - 1) / countPerPage);

    var html = new Array();
    if (curPage == 1) {
        html.push("<span>首页</span><span>上一页</span>");
    }
    else {
        html.push("<a href=\"javascript:GoToPage(1)\">首页</a><a href=\"javascript:GoToPage(" + (curPage - 1) + ")\">上一页</a>");
    }

    if (parseInt((curPage - 1) / countPerPage) > 0) {
        html.push("<a href=\"javascript:GoToPage(" + ((curLinkPage - 1) * countPerPage + countPerPage) + ")\">上" + countPerPage + "页</A>");
    }

    for (var i = (curLinkPage * countPerPage); i < (curLinkPage * countPerPage + countPerPage); ++i) {
        if (totalPage > i) {
            if (i == (curPage - 1)) {
                html.push("<a href=\"javascript:GoToPage(" + (i + 1) + ")\" class=\"here\">" + (i + 1) + "</a>");
            }
            else {
                html.push("<a href=\"javascript:GoToPage(" + (i + 1) + ")\" >" + (i + 1) + "</a>");
            }
        }
    }

    //下X页的版面数量
    var indexCount = parseInt(totalPage % countPerPage > 0 ? totalPage / countPerPage + 1 : totalPage / countPerPage);
    //最后一个版面的最小页数
    var minPage = countPerPage * (indexCount - 1) + 1;

    if ((curPage) < minPage)//如果当前页小于最后一个版面的页数，就显示下X页
    {
        html.push("<a   href=\"javascript:GoToPage(" + ((curLinkPage + 1) * countPerPage + 1) + ")\">下" + countPerPage + "页</A>");
    }

    if (curPage == totalPage) {
        html.push("<span>下一页</span><span>尾页</span>");
    }
    else {
        html.push("<a href=\"javascript:GoToPage(" + (curPage + 1) + ")\">下一页</a><a href=\"javascript:GoToPage(" + totalPage + ")\">尾页</a>");
    }

    html.push("<span style=\"color:#000\">共" + totalCount + "条记录</span>");

    return html.join("");
}


//转到指定的页面上；
function GoToPage(pageNo) {
    SelectGoods.CurrentInstance.CurPage = pageNo;
    SelectGoods.CurrentInstance.IsFirstSearch = true;
    SelectGoods.CurrentInstance.GenerateDivContent();
}




























